import { _decorator, AnimationClip, Animation, Component, Node } from "cc"
import { FSM_PARAMS_TYPE_ENUM, PARAME_NAME_ENUM } from "../../Enums"
import State from "../../base/state"
import { StateMachine, getParamsNumber, getParamsTrigger } from "../../base/StateMachine"
import IdleSubStateMachine from "./IdleSubStateMachine"
import TurnLeftSubStateMachine from "./TurnLeftSubStateMachine"

const { ccclass, property } = _decorator

@ccclass("PlayerStateMachine")
export class PlayerStateMachine extends StateMachine {
  async init() {
    this.animationComp = this.addComponent(Animation)
    this.initParams()
    this.initStateMachines()
    this.initAnimationEvent()
    await Promise.all(this.waitingList)
  }
  run(params, value) {
    if (value) {
      this.currentState = this.stateMachines.get(params)
    } else {
      this.currentState = this.stateMachines.get(PARAME_NAME_ENUM.IDLE)
    }
  }
  initParams() {
    this.params.set(PARAME_NAME_ENUM.IDLE, getParamsTrigger())
    this.params.set(PARAME_NAME_ENUM.TURNLEFT, getParamsTrigger())
    this.params.set(PARAME_NAME_ENUM.DIRECTION, getParamsNumber())
  }
  initStateMachines() {
    // this.stateMachines.set(
    //   PARAME_NAME_ENUM.IDLE,
    //   new State(this, "texture/player/idle/top", AnimationClip.WrapMode.Loop)
    // )
    // this.stateMachines.set(PARAME_NAME_ENUM.TURNLEFT, new State(this, "texture/player/turnleft/top/turnleft"))
    this.stateMachines.set(PARAME_NAME_ENUM.IDLE, new IdleSubStateMachine(this))
    this.stateMachines.set(PARAME_NAME_ENUM.TURNLEFT, new TurnLeftSubStateMachine(this))
  }
  initAnimationEvent() {
    this.animationComp.on(Animation.EventType.FINISHED, () => {
      const name = this.animationComp.defaultClip.name
      const whiteList = ["turn"]
      if (whiteList.some(v => name.includes(v))) {
        this.setParams(PARAME_NAME_ENUM.IDLE, true)
      }
    })
  }

  update(deltaTime: number) {}
}
